#ifndef CT_VOIP_LOSTPACKET_MANAGER_H_
#define CT_VOIP_LOSTPACKET_MANAGER_H_

#include <queue>
#include <ext/hash_map>
#include "ct_voip_lost_pkt.h"
#include "ctSingleton.h"
#include "ct_mutex.h"
#include "command.h"

// #define TEST_VOIP_LOST_PACKET_
#define MAX_PACKET_NUM          1024
#define RTP_PACKET_HEAD_LEN     12

class ctVOIPLostPacketThread;
class ctVOIPLostPacketManager : public Singleton<ctVOIPLostPacketManager> {
friend class ctVOIPLostPacketThread;

public:
        struct ctPacket {
                ctPacket() {
                        user_id_ = 0;
                        seq_no_ = 0;
                }

                ctPacket(DWORD userid, DWORD seqno) {
                        user_id_ = userid;
                        seq_no_ = seqno;
                }

                DWORD user_id_;
                DWORD seq_no_;
        };

        typedef std::queue<ctPacket> ctPacketQueue;
        typedef __gnu_cxx::hash_map<DWORD, ctVOIPLostPacket *> ctLostPacketMap;
        typedef __gnu_cxx::hash_map<DWORD, WORD> ctLostRateMap;

public:
        ctVOIPLostPacketManager();
        ~ctVOIPLostPacketManager();

        bool init();
        void final();
        void push(DWORD userid, DWORD seqno);
        int getLostRate(DWORD userid, WORD &lostrate);
        void printLostRate();

protected:
        void process();
        void checkTimeOut();
        void calculateLostRate();
        void updateLostRateMap();

protected:
        volatile bool is_start_thread_;
        mutable ctMutex lock_queue_packet_;
        ctPacketQueue queue_packet_;
        ctPacketQueue queue_tmp_;
        ctLostPacketMap map_lost_packet_;
        mutable ctMutex lock_map_lost_rate_;
        ctLostRateMap map_lost_rate_;
};
#endif // CT_VOIP_LOSTPACKET_MANAGER_H_
